package com.dk.controller;

import com.alibaba.druid.util.StringUtils;
import com.dk.pojo.EvolveReply;
import com.dk.service.EvolveReplyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 人工智能回复
 */
@Controller
@RequestMapping("evolveReply")
public class EvolveReplyController {
    @Autowired
    private EvolveReplyService evolveReplyService;
    @Autowired
    private RedisTemplate redisTemplate;

    public final static String EvolveReplyController = "system_hou_Evolve_Reply_Controller";

    @RequestMapping("toShow")
    public String toShow(){
        return "/evolveReply/show";
    }


    @RequestMapping("getInfo")
    @ResponseBody
    public Map<String,Object> getInfoServer(EvolveReply evolveReply,
                                            @RequestParam(defaultValue = "1")Integer pageNumber,
                                            @RequestParam(defaultValue = "3")Integer pageSize){
        EvolveReply evolve = new EvolveReply();
        List<EvolveReply> list = new ArrayList<>();
        //条件查询出数据
        PageInfo<EvolveReply> page =  evolveReplyService.show(evolveReply,pageNumber,pageSize);
        //查询总条数
        Integer countAll = evolveReplyService.getCount();
        String ekiKeyId = evolveReply.getEkiKeyId();
        String EvolveReplyControllerEkiKeyId = null;
        if (!StringUtils.isEmpty(ekiKeyId)){
            //字符串拼接出一个唯一的key
            EvolveReplyControllerEkiKeyId = new StringBuilder(EvolveReplyController).append(ekiKeyId).toString();
            //根据key去redis获取值
            if (redisTemplate.opsForValue().get(EvolveReplyControllerEkiKeyId) != null){
                String onceonce = (String) redisTemplate.opsForValue().get(EvolveReplyControllerEkiKeyId);
                //如果不加这行代码的话，会导致58行代码无法判断，因为数据存进redis再取出来，即使是一样的字符串，也无法equals，这行代码的效果是反序列化（解决掉无效字符串）
                //同样的字符串“0”，存进redis中再取出来，地址值就变成了000000048，但正常的是48，用replaceAll("\\p{C}", "")处理一下就没问题了
                String s = onceonce.replaceAll("\\p{C}", "");
                Integer integer = Integer.valueOf(s);
                if (!s.equals("3")){
                    redisTemplate.opsForValue().set(EvolveReplyControllerEkiKeyId,String.valueOf(integer + 1),10, TimeUnit.SECONDS);
                }else {
                    evolve.setErValue("那啥……你先冷静10秒钟……特么的一直问这同一个问题你真的不会吐吗？");
                    list.add(evolve);
                    page.setList(list);
                    //需要total:总条数 rows:结果集 (都是key,value结合,所以用map)
                    Map<String,Object> map = new HashMap<>();
                    map.put("total",page.getTotal());
                    map.put("rows",page.getList());
                    return map;
                }
            }else {
                redisTemplate.opsForValue().set(EvolveReplyControllerEkiKeyId,"0",10);
            }
        }

        //如果为空，则让对方添加
        if (page.getList().isEmpty()){
            evolve.setErValue("尚未学习该回复，请点击正上方按钮 - ↑ - ↑ - ↑ - 提升智能化");
            list.add(evolve);
            page.setList(list);
        }else {
            List<EvolveReply> lists = page.getList();
            //如果查找出来的条数和总条数相等，说明没有查询条件，就让它完全显示，否则就按照查询条件来
            if (lists.size() != countAll){
                //如果条件查询到多条，则随机选一条回复
                EvolveReply evolveReplyToo = lists.get((int) (0 + Math.random() * (page.getList().size())));
                list.add(evolveReplyToo);
                page.setList(list);
            }
        }
        //需要total:总条数 rows:结果集 (都是key,value结合,所以用map)
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getList());
        return map;
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public boolean addInfo(EvolveReply evolveReply){
        return evolveReplyService.addInfo(evolveReply);
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "/evolveReply/add";
    }

    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id, Model model){
        EvolveReply evolveReply = evolveReplyService.toUpdatet(id);
        model.addAttribute("evolveReply",evolveReply);
        return "/evolveReply/update";
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(EvolveReply evolveReply){
        return evolveReplyService.updateInfo(evolveReply);
    }

    @RequestMapping("deleteInfo/{id}")
    @ResponseBody
    public boolean deleteInfo(@PathVariable("id")Integer id){
        return evolveReplyService.deleteInfo(id);
    }



}
